package com.gmu.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.gmu.mapper.TblUnitsMapper;
import com.gmu.pojo.TblRole;
import com.gmu.pojo.TblUnits;
import com.gmu.pojo.excel.UnitsData;

import java.util.List;
import java.util.Objects;

/**
 * @author 木鱼~
 * @version 1.0
 */
public class UnitsListener extends AnalysisEventListener<UnitsData> {

    public TblUnitsMapper tblUnitsMapper;

    public List<TblRole> roleList;

    /**
     * 如果使用了spring,请使用这个构造方法。每次创建Listener的时候需要把spring管理的类传进来
     */
    public UnitsListener(TblUnitsMapper tblUnitsMapper,List<TblRole> roleList) {
        this.tblUnitsMapper = tblUnitsMapper;
        this.roleList = roleList;
    }

    /**
     * 这个每一条数据解析都会来调用
     */
    @Override
    public void invoke(UnitsData unitsData, AnalysisContext context) {
        TblUnits topUnits = tblUnitsMapper.getTopUnits(unitsData.getLevel_1());
        if (!Objects.isNull(topUnits)){
            if (unitsNextNotNull(unitsData.getLevel_2())){
                TblUnits units2 = getByName(unitsData.getLevel_2(), topUnits.getUnitsId());
                if (!Objects.isNull(units2) && unitsNextNotNull(unitsData.getLevel_3())){
                    TblUnits units3 = getByName(unitsData.getLevel_3(), units2.getUnitsId());
                    if (!Objects.isNull(units3) && unitsNextNotNull(unitsData.getLevel_4())){
                        TblUnits units4 = getByName(unitsData.getLevel_4(), units3.getUnitsId());
                        if (!Objects.isNull(units4) && unitsNextNotNull(unitsData.getLevel_5())){
                            TblUnits units5 = getByName(unitsData.getLevel_5(), units4.getUnitsId());
                            if (!Objects.isNull(units5) && unitsNextNotNull(unitsData.getLevel_6())){
                                TblUnits units6 = getByName(unitsData.getLevel_6(), units5.getUnitsId());
                                if (!Objects.isNull(units6) && unitsNextNotNull(unitsData.getLevel_7())){
                                    TblUnits units7 = getByName(unitsData.getLevel_7(), units6.getUnitsId());
                                    if (!Objects.isNull(units7) && unitsNextNotNull(unitsData.getLevel_8())){
                                        TblUnits units8 = getByName(unitsData.getLevel_8(), units7.getUnitsId());
                                        if (Objects.isNull(units8)){
                                            addUnits(unitsData.getLevel_8(),unitsData.getIsSampleUnits(),unitsData.getRole(),unitsData.getPeoples(),units7.getUnitsId());
                                        }
                                    }else {
                                        if (Objects.isNull(units7))
                                            addUnits(unitsData.getLevel_7(), unitsData.getIsSampleUnits(), unitsData.getRole(), unitsData.getPeoples(), units6.getUnitsId());
                                    }
                                }else {
                                    if (Objects.isNull(units6))
                                        addUnits(unitsData.getLevel_6(), unitsData.getIsSampleUnits(), unitsData.getRole(), unitsData.getPeoples(), units5.getUnitsId());
                                }
                            }else {
                                if (Objects.isNull(units5))
                                    addUnits(unitsData.getLevel_5(), unitsData.getIsSampleUnits(), unitsData.getRole(), unitsData.getPeoples(), units4.getUnitsId());
                            }
                        }else {
                            if (Objects.isNull(units4))
                                addUnits(unitsData.getLevel_4(), unitsData.getIsSampleUnits(), unitsData.getRole(), unitsData.getPeoples(), units3.getUnitsId());
                        }
                    }else {
                        if (Objects.isNull(units3))
                            addUnits(unitsData.getLevel_3(), unitsData.getIsSampleUnits(), unitsData.getRole(), unitsData.getPeoples(), units2.getUnitsId());
                    }
                }else {
                    if (Objects.isNull(units2))
                        addUnits(unitsData.getLevel_2(), unitsData.getIsSampleUnits(), unitsData.getRole(), unitsData.getPeoples(), topUnits.getUnitsId());
                }
            }
        }else {
            System.out.println("未找到最高级单位!");
        }


    }

    public void addUnits(String name,String sampleStatus,String role,String unitsPeoples,Integer parentId){
        TblUnits tblUnits = new TblUnits();
        int index = roleList.indexOf(new TblRole(role));
        if (index < 0){
            tblUnits.setRoleId(0);
        }else {
            tblUnits.setRoleId(roleList.get(index).getRoleId());
        }
        tblUnits.setName(name);
        tblUnits.setSampleStatus(sampleStatus.equals("否") ? 0 : 1);
        tblUnits.setUnitsPeoples(Integer.parseInt(unitsPeoples));
        tblUnits.setParentId(parentId);
        tblUnitsMapper.addUnits(tblUnits);
    }

    public TblUnits getByName(String name,Integer parentId){
        return tblUnitsMapper.getOtherUnits(name,parentId);
    }

    public boolean unitsNextNotNull(String unitsName){
        return unitsName != null && !unitsName.equals("");
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
